<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库编号" prop="warehousingNo">
        <el-input
          v-model="queryParams.warehousingNo"
          placeholder="请输入入库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库时间" prop="warehousingTime">
        <el-date-picker clearable
          v-model="queryParams.warehousingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:receiptOorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:receiptOorder:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:receiptOorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库编号" align="center" prop="warehousingNo" />
      <el-table-column label="入库时间" align="center" prop="warehousingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warehousingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务类型" align="center" prop="businessType" />
      <el-table-column label="入库类型" align="center" prop="receiptType" />
      <el-table-column label="入库仓库" align="center" prop="warehousingWarehouseName" />
      <el-table-column label="责任人名称" align="center" prop="chargePersonName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.orderStatus==1">待入库</el-button>
          <el-button type="text" v-if="scope.row.orderStatus==2">已入库</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:receiptOorder:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:receiptOorder:delete']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleProduct(scope.row)"
          >关联产品</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【入库订单】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1071px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px" inline>
        <el-form-item label="入库编号" prop="warehousingNo">
          <el-input v-model="form.warehousingNo" placeholder="请输入入库编号" />
        </el-form-item>
        <el-form-item label="入库时间" prop="warehousingTime">
          <el-date-picker clearable
            v-model="form.warehousingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="业务类型" prop="productType">
          <el-select v-model="form.businessType" placeholder="业务类型">
            <el-option
              v-for="item in businessTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入库类型" prop="receiptType">
          <el-select v-model="form.receiptType" placeholder="入库类型">
            <el-option
              v-for="item in receiptTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="入库仓库" prop="warehousingWarehouseName">
          <el-input style="width: 300px" v-model="form.warehousingWarehouseName" readonly placeholder="">
            <template slot="append">
              <el-button type="primary" style="width: 120px" @click="openImportWarehouse">
                选择仓库
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="产品关联" prop="productListStr">
          <div >
            <el-form  label-width="120px"  >

              <el-button type="primary" size="mini" @click="openProduct" style="margin-bottom: 10px">选择产品</el-button>
              <!--              <div v-for="(item,index) in  models" :key="index" style="margin-top: 10px">-->
              <div style="margin-top: 10px">
                <el-form-item  label-width="0">
                  <div style="display:flex;">
                    <label class="whiteBankCard"
                           style="width: 100px;margin-right: 20px">整机品名</label>
                    <label class="whiteBankCard"
                           style="width: 100px;margin-right: 10px">型号</label>

                    <label class="whiteBankCard"
                           style="width: 200px;margin-right: 10px">选择仓库</label>
                    <label class="whiteBankCard"
                           style="width: 100px; ">入库数量</label>

                    <label class="whiteBankCard"
                           style="width: 120px;margin-right: 10px">库存数量</label>
                    <label class="whiteBankCard"
                           style="width: 100px;margin-right: 10px">单位</label>
<!--                    <label class="whiteBankCard"-->
<!--                           style="width: 100px;margin-right: 10px">备注</label>-->
                    <label class="whiteBankCard"
                           style="width: 80px;margin-right: 10px">操作</label>

                  </div>

                  <div style="display:flex;" v-for="(item,index) in  productList"   :key="item.id">
                    <div :prop="'product.' + index">
                      <el-input class="whiteBankCard" readonly  v-model="item.productName"   placeholder="整机品名"
                                style="width: 100px;margin-right: 10px"></el-input>
                      <el-input class="model" readonly v-model="item.model" placeholder="型号" style="width: 100px;margin-right: 10px"></el-input>

                      <el-input v-model="item.warehousingWarehouseName" readonly placeholder="仓库名"
                                style="width: 200px;margin-right: 10px">
                        <template slot="append">
                          <el-button icon="el-icon-search" circle @click="openImportWarehouse(index)"></el-button>
                        </template>
                      </el-input>
                      <el-input class="whiteBankCard" v-model="item.receiptQuantity"  placeholder="入库数量"
                                style="width: 100px;margin-right: 10px"/>

                      <el-input class="whiteBankCard" v-model="item.inventoryQuantity" readonly placeholder="库存数量"
                                style="width: 100px;margin-right: 10px"/>
                      <el-input class="whiteBankCard" readonly v-model="item.unit" placeholder="单位"
                                style="width: 100px;margin-right: 10px"/>
<!--                      <el-input class="whiteBankCard" v-model="item.remark" placeholder="备注"-->
<!--                                style="width: 100px;margin-right: 10px"/>-->
                      <el-button type="danger" size="mini"  style="height: 30px" @click="removeProduct(index)" >
                        删除
                      </el-button>
                    </div>
                  </div>
                </el-form-item>
              </div>

            </el-form>
          </div>
        </el-form-item>
        <el-form-item label="负责人" prop="chargePersonName">
          <el-input v-model="form.chargePersonName" readonly style="width: 200px">
            <template slot="append">
              <el-button type="primary" style="width: 120px" @click="openImportUser">
                选择负责人
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea"   style="width: 300px;"   size="medium" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="productTitle" :visible.sync="productOpen" width="750px" >
<!--      <div style="margin-top: 10px">-->
<!--        <div style="display:flex;">-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">整机品名</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">型号</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">单位</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">仓库</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 150px;margin-right: 10px">入库数量</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">备注</label>-->
<!--        </div>-->
<!--        <div style="display:flex; margin-top: 10px" v-for="(item,index) in  productList"   :key="index" >-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">{{ item.productName }}</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">{{ item.model }}</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">{{ item.unit }}</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">{{ item.warehousingWarehouseName }}</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px;color: #00afff">{{ item.receiptQuantity }}</label>-->
<!--          <label class="whiteBankCard"-->
<!--                 style="width: 100px;margin-right: 10px">{{ item.remark }}</label>-->
<!--        </div>-->
<!--      </div>-->
<!--      关联产品表格-->
      <el-table
        :data="productList"
        style="width: 100%"
        height="250">
        <el-table-column
          fixed
          prop="productName"
          label="产品名称"
          width="150">
        </el-table-column>
        <el-table-column
          prop="model"
          label="型号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
          width="120">
        </el-table-column>
        <el-table-column
          prop="warehousingWarehouseName"
          label="仓库"
          width="120">
        </el-table-column>
        <el-table-column
          prop="receiptQuantity"
          label="入库数量"
          width="120">

        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          width="150">
        </el-table-column>
      </el-table>
    </el-dialog>
    <import-user ref="import" @showUser="showUser"></import-user>
    <import-warehouse ref="importWarehouse" @showWarehouse="showWarehouse"></import-warehouse>
    <import-product ref="importProduct" @showProduct="showProduct"></import-product>
  </div>
</template>
<style>
.el-input-group {
  line-height: normal;
  display: inline-table;
  width: 30%;
  border-collapse: separate;
  border-spacing: 0;
}
</style>
<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/module/receiptOrder";
import {getDicts} from "@/api/system/dict/data";
import importWarehouse from "@/views/module/product/warehouse";
import importUser from "@/views/module/customer/user";
import importProduct from "@/views/dialog/product";

export default {
  name: "Order",
  components:{importWarehouse,importUser,importProduct},
  data() {
    return {
      // 弹出层标题
      productTitle: "",
      // 是否显示弹出层
      productOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【入库订单】表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehousingNo: null,
        warehousingTime: null,
        businessType: null,
        receiptType: null,
        warehousingWarehouseId: null,
        warehousingWarehouseName: null,
        chargePersonId: null,
        chargePersonName: null,
        orderStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      businessTypeOptions: [], // 业务类型
      receiptTypeOptions: [], // 入库类型
      productList: []
    };
  },
  created() {
    this.getList();
    this.getBusinessTypeOptions();
    this.getReceiptTypeOptions();
  },
  methods: {
    /** 查看关联产品 */
    handleProduct(row) {
      this.productList=row.productList;
      this.productOpen = true;
      this.productTitle = "关联产品";
    },
    /** 打开产品弹窗 */
    openProduct() {
      this.$refs.importProduct.show();
    },
    showProduct(data){
      debugger
      this.productList.push(...data);//数据添加元素
    },
    // removeProduct(index) {
    //   this.productList.splice(index, 1)
    // },
    //删除关联产品按钮
    removeProduct(val){
     let index= this.productList.findIndex(item=>{
        return item.id===val
      })
      console.log(index)
      this.productList.splice(index,1)
    },
    /** 打开负责人弹窗 */
    openImportUser() {
      this.$refs.import.show();
    },
    showUser(data){
      //负责人
      this.form.chargePersonId=data.userId;
      this.form.chargePersonName=data.userName;
    },
    /** 打开仓库弹窗 */
    openImportWarehouse(index) {
      this.$refs.importWarehouse.show(index);
    },
    showWarehouse(data){
      debugger
      //仓库
      if(typeof data.index === 'number' ){
        this.productList[data.index].warehousingWarehouseName=data.name;
        this.productList[data.index].warehousingWarehouseId=data.id;
      }else{
        this.form.warehousingWarehouseId=data.id;
        this.form.warehousingWarehouseName=data.name;
         this.productList.forEach(item=>{
           item.warehousingWarehouseName=data.name
           item.warehousingWarehouseId=data.id
         })
      }
    },
    // 业务类型
    getBusinessTypeOptions() {
      getDicts('biz_business_type').then(response => {
        this.businessTypeOptions = response.data
      })
    },
    // 入库类型
    getReceiptTypeOptions() {
      getDicts('biz_receipt_type').then(response => {
        this.receiptTypeOptions = response.data
      })
    },
    getWarehousingNo (){
      var date = new Date();
      var year = date.getFullYear();        //年 ,从 Date 对象以四位数字返回年份
      var month = date.getMonth() + 1;      //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
      var day = date.getDate();             //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
      //修改月份格式
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      //修改日期格式
      if (day >= 0 && day <= 9) {
        day = "0" + day;
      }

      let round = Math.floor(Math.random() * (999 - 100)) + 100;
      var currentFormatDate = `${year}${month}${day}`;
      return "QTRK"+currentFormatDate+round;
    },
    /** 查询【入库订单】列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        // 产品类型判断
        let arr = this.orderList;
        arr.forEach(item => {
          item.businessType = this.getValue(this.businessTypeOptions, item.businessType);
          item.receiptType = this.getValue(this.receiptTypeOptions, item.receiptType)
        });
        this.orderList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehousingNo: null,
        warehousingTime: null,
        businessType: null,
        receiptType: null,
        warehousingWarehouseId: null,
        warehousingWarehouseName: null,
        chargePersonId: null,
        chargePersonName: null,
        remark: null,
        orderStatus: "0",
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null,
        delFlag: null
      };
      this.resetForm("form");
      this.productList=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加入库订单";
      this.form.warehousingNo=this.getWarehousingNo();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.productList=row.productList;
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入库订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.productList=this.productList;
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【入库订单】编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
